extends EnemyBase

enum State {
	IDLE,
	PATROL,
	CHASE,
	ATTACK1,  # 普通攻击
	ATTACK2,  # 翻滚攻击
	ATTACK3,  # 扔炸弹
	HURT,
	DEAD
}

@export var attack1_damage: float = 20.0
@export var attack2_damage: float = 15.0
@export var bomb_damage: float = 30.0
@export var attack_cooldown: float = 2.0
@export var patrol_range: float = 100.0
@export var bomb_scene: PackedScene  # 炸弹场景
@export var health_bar_scene: PackedScene = preload("res://scenes/monster_health_bar.tscn")
@export var detection_range: float = 300.0  # 视野范围
@export var chase_memory_time: float = 2.0  # 失去目标后继续追击的时间
@export var melee_attack_range: float = 60.0  # 降低近战攻击范围，确保需要更近的距离才能攻击
@export var bomb_attack_range: float = 250.0  # 保持炸弹攻击范围
@export var max_chase_range: float = 300.0   # 最大追击范围
@export var preferred_attack_distance: float = 50.0  # 降低理想攻击距离
@export var distance_tolerance: float = 10.0  # 降低距离容差范围
@export var attack_damage_check_frames: int = 20  # 增加到20帧，约1/3秒，增加攻击检测时间

var current_state: State = State.IDLE
var target: Node2D = null
var patrol_location: Vector2 = Vector2.ZERO
var initial_position: Vector2 #初始方向
var can_attack: bool = true #是否可攻击
var facing_right: bool = true #面向右边
var attack_type: int = 1  # 1: 普通攻击, 2: 翻滚攻击, 3: 扔炸弹
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var knockback_force = Vector2.ZERO
var is_invincible = false
var invincible_timer = 0.0
const INVINCIBLE_TIME = 0.5
var attack_ready = false
var last_target_position: Vector2 = Vector2.ZERO  # 最后看到目标的位置
var chase_memory_timer: float = 0.0  # 追击记忆计时器
var bomb_queued = false  # 添加变量来标记是否需要扔炸弹
var attack1_damage_check_timer: int = 0  # 攻击1伤害检测计时器
var attack2_damage_check_timer: int = 0  # 攻击2伤害检测计时器
var attack1_damage_dealt: bool = false  # 攻击1是否已造成伤害
var attack2_damage_dealt: bool = false  # 攻击2是否已造成伤害

@onready var attack_hitbox: Area2D = $AttackHitbox
@onready var hurt_box: Area2D = $HurtBox

func _ready():
	super._ready()  # 调用父类的_ready
	initial_position = global_position
	_update_patrol_location()
	animated_sprite.play("idle")
	
	# 设置碰撞层
	collision_layer = 4  # 敌人层 (第3层)
	collision_mask = 3  # 与地面层(2)和玩家层(1)碰撞，但不会被玩家推动
	
	# 设置攻击检测区域的碰撞层
	if attack_hitbox:
		attack_hitbox.collision_layer = 8  # 敌人攻击层 (第4层)
		attack_hitbox.collision_mask = 1   # 只检测玩家层 (第1层)
	
	# 设置受伤检测区域的碰撞层
	if hurt_box:
		hurt_box.collision_layer = 16  # 敌人受伤层 (第5层)
		hurt_box.collision_mask = 32   # 检测玩家攻击层 (第6层)
	
	# 创建血条
	_setup_health_bar()

func _setup_health_bar():
	health_bar = health_bar_scene.instantiate()
	add_child(health_bar)
	health_bar.position = Vector2(-20, -30)  # 调整血条位置

func _physics_process(delta):
	if is_invincible:
		invincible_timer -= delta
		if invincible_timer <= 0:
			is_invincible = false
			modulate.a = 1.0
	
	# 更新追击记忆计时器
	if chase_memory_timer > 0:
		chase_memory_timer -= delta
	
	# 更新攻击伤害检测计时器
	if attack1_damage_check_timer > 0:
		attack1_damage_check_timer -= 1
		if attack1_damage_check_timer <= 0:
			attack1_damage_dealt = false  # 重置伤害标记
	
	if attack2_damage_check_timer > 0:
		attack2_damage_check_timer -= 1
		if attack2_damage_check_timer <= 0:
			attack2_damage_dealt = false  # 重置伤害标记
	
	# 应用重力
	if not is_on_floor():
		velocity.y += gravity * delta
		# 如果掉出地图太远，重置位置
		if global_position.y > 1000:  # 根据你的地图大小调整这个值
			global_position = initial_position
			velocity = Vector2.ZERO
	else:
		# 在地面上时重置垂直速度
		velocity.y = 0
	
	# 处理击退力
	if knockback_force != Vector2.ZERO:
		velocity = knockback_force
		knockback_force = knockback_force.move_toward(Vector2.ZERO, delta * 1000)
		# 如果击退力很小，直接清零
		if knockback_force.length() < 10:
			knockback_force = Vector2.ZERO
	
	# 根据当前状态处理行为
	match current_state:
		State.IDLE:
			_process_idle_state(delta)
		State.PATROL:
			_process_patrol_state(delta)
		State.CHASE:
			_process_chase_state(delta)
		State.ATTACK1:
			_process_attack1_state(delta)
		State.ATTACK2:
			_process_attack2_state(delta)
		State.ATTACK3:
			_process_attack3_state(delta)
		State.HURT:
			_process_hurt_state(delta)
		State.DEAD:
			_process_dead_state(delta)
	
	# 更新朝向 - 修改为根据情况决定朝向
	if current_state == State.ATTACK1 or current_state == State.ATTACK3:
		# 攻击状态下，始终面向玩家
		if target:
			facing_right = target.global_position.x > global_position.x
			animated_sprite.flip_h = !facing_right
	elif current_state == State.ATTACK2:
		# 翻滚攻击状态下，根据移动方向决定朝向
		if velocity.x != 0:
			facing_right = velocity.x > 0
			animated_sprite.flip_h = !facing_right
	elif velocity.x != 0:
		# 移动状态下，根据移动方向决定朝向
		facing_right = velocity.x > 0
		animated_sprite.flip_h = !facing_right
	
	# 确保移动和碰撞检测
	move_and_slide()
	
	# 额外的地面检测
	if is_on_floor():
		velocity.y = 0  # 确保在地面上时垂直速度为0
	
	# 处理持续伤害检测
	if attack1_damage_check_timer > 0 and not attack1_damage_dealt and target and current_state == State.ATTACK1:
		_check_attack1_damage()
	
	if attack2_damage_check_timer > 0 and not attack2_damage_dealt and target and current_state == State.ATTACK2:
		_check_attack2_damage()
	
	# 调试输出
	if Engine.get_physics_frames() % 60 == 0:  # 每60帧输出一次
		print("琥珀哥布林状态: ", State.keys()[current_state], ", 速度: ", velocity, ", 位置: ", global_position)

func _process_idle_state(_delta):
	if not is_on_floor():
		return
		
	velocity.x = 0
	if target:
		current_state = State.CHASE
		animated_sprite.play("run")
	elif randf() < 0.01:
		current_state = State.PATROL
		animated_sprite.play("run")
		_update_patrol_location()
	elif not animated_sprite.animation == "idle":
		animated_sprite.play("idle")

func _process_patrol_state(_delta):
	if not is_on_floor():
		return
		
	if target:
		current_state = State.CHASE
		animated_sprite.play("run")
		return
	
	var direction = (patrol_location - global_position).normalized()
	velocity.x = direction.x * move_speed
	
	# 防止原地踏步 - 如果距离巡逻点还有一段距离但速度接近0，强制设置速度
	if global_position.distance_to(patrol_location) > 20 and abs(velocity.x) < 10:
		velocity.x = direction.x * move_speed
		print("修正琥珀哥布林巡逻速度: ", velocity.x)
	
	if global_position.distance_to(patrol_location) < 10:
		current_state = State.IDLE
		animated_sprite.play("idle")
		velocity.x = 0  # 确保停止移动

func _process_chase_state(delta):
	if not target and chase_memory_timer <= 0:
		current_state = State.IDLE
		animated_sprite.play("idle")
		velocity.x = 0
		return
	
	var target_pos = target.global_position if target else last_target_position
	var direction = (target_pos - global_position).normalized()
	var distance = global_position.distance_to(target_pos)
	
	# 如果超出最大追击范围，返回空闲状态
	if distance > max_chase_range:
		current_state = State.IDLE
		animated_sprite.play("idle")
		target = null
		chase_memory_timer = 0
		return
	
	# 更新目标位置和追击记忆
	if target:
		if distance <= detection_range:
			last_target_position = target.global_position
			chase_memory_timer = chase_memory_time
	
	# 处理攻击逻辑
	if can_attack and target:
		if distance <= melee_attack_range:  # 近战攻击范围内
			# 停止移动并直接攻击
			velocity.x = 0
			attack_type = randi_range(1, 2)  # 随机选择近战攻击类型
			current_state = State.ATTACK1 if attack_type == 1 else State.ATTACK2
			animated_sprite.play("attack" + str(attack_type))
			attack_ready = false
			
			# 攻击前确保面向玩家
			facing_right = target.global_position.x > global_position.x
			animated_sprite.flip_h = !facing_right
			return
		elif distance <= bomb_attack_range:  # 炸弹攻击范围内
			# 直接使用炸弹攻击，不再追近
			velocity.x = 0
			attack_type = 3
			current_state = State.ATTACK3
			animated_sprite.play("attack3")
			attack_ready = false
			
			# 攻击前确保面向玩家
			facing_right = target.global_position.x > global_position.x
			animated_sprite.flip_h = !facing_right
			return
	
	# 追击逻辑 - 修改为保持一定距离
	var ideal_distance = preferred_attack_distance  # 理想攻击距离
	
	if distance > bomb_attack_range:
		# 如果超出攻击范围，正常追击
		velocity.x = direction.x * move_speed
		if not animated_sprite.animation == "run":
			animated_sprite.play("run")
	elif distance < ideal_distance * 0.8:
		# 如果太近，后退 - 面向后退方向
		velocity.x = -direction.x * move_speed * 0.7  # 后退速度稍慢
		if not animated_sprite.animation == "run":
			animated_sprite.play("run")
	elif distance > ideal_distance * 1.2:
		# 如果太远但在攻击范围内，缓慢接近
		velocity.x = direction.x * move_speed * 0.5  # 缓慢接近
		if not animated_sprite.animation == "run":
			animated_sprite.play("run")
	else:
		# 在理想距离范围内，停止移动
		velocity.x = 0
		if not animated_sprite.animation == "idle":
			animated_sprite.play("idle")
	
	# 地面检测
	if not is_on_floor():
		velocity.y += gravity * delta
	else:
		velocity.y = 5  # 轻微的向下力保持贴地

func _process_attack1_state(_delta):
	velocity = Vector2.ZERO  # 确保攻击时不移动
	
	# 检查动画是否正在播放attack1
	if not animated_sprite.animation == "attack1":
		animated_sprite.play("attack1")
		attack_ready = false
		attack1_damage_dealt = false  # 重置伤害标记
		return
	
	# 在第4帧（索引3）处准备攻击
	if not attack_ready and animated_sprite.frame >= 3:
		attack_ready = true
		_perform_attack1()
	
	# 如果动画播放完毕但状态仍然是ATTACK1，则转到CHASE状态
	if animated_sprite.frame >= animated_sprite.sprite_frames.get_frame_count("attack1") - 1:
		current_state = State.CHASE
		animated_sprite.play("run")

func _process_attack2_state(_delta):
	# 检查动画是否正在播放attack2
	if not animated_sprite.animation == "attack2":
		animated_sprite.play("attack2")
		attack_ready = false
		attack2_damage_dealt = false  # 重置伤害标记
		return
	
	# 在第4帧（索引3）处准备攻击
	if not attack_ready and animated_sprite.frame >= 3:
		attack_ready = true
		_perform_attack2()
	
	# 确保翻滚过程中保持移动，直到动画后半段
	if animated_sprite.frame < 7:
		# 如果速度为零，重新设置速度
		if velocity.x == 0 and target:
			var attack_direction = (target.global_position - global_position).normalized()
			velocity.x = attack_direction.x * move_speed * 2
	# 在动画后半段逐渐减速
	else:
		velocity = velocity.move_toward(Vector2.ZERO, _delta * 1000)
	
	# 确保动画播放完后回到追击状态
	if animated_sprite.frame >= animated_sprite.sprite_frames.get_frame_count("attack2") - 1:
		current_state = State.CHASE
		animated_sprite.play("run")
		can_attack = false
		$AttackTimer.start(attack_cooldown)

func _process_attack3_state(_delta):
	# 确保水平方向停止
	velocity.x = 0
	
	# 如果有目标，确保面向目标
	if target:
		facing_right = target.global_position.x > global_position.x
		animated_sprite.flip_h = !facing_right
	
	# 如果在空中，继续应用重力
	if not is_on_floor():
		velocity.y += gravity * _delta
	else:
		# 在地面上时确保不会浮空
		velocity.y = 5  # 添加一个很小的向下的力，确保贴地
	
	# 检查动画是否正在播放attack3
	if not animated_sprite.animation == "attack3":
		animated_sprite.play("attack3")
		attack_ready = false
		bomb_queued = true  # 标记需要扔炸弹
		# 记录目标位置，避免后续目标消失导致炸弹未能实例化
		last_target_position = target.global_position if target else last_target_position
		return
	
	# 在特定帧时标记准备扔炸弹
	if not attack_ready and animated_sprite.frame >= 5:
		attack_ready = true

func _process_hurt_state(_delta):
	velocity = Vector2.ZERO
	if not animated_sprite.animation == "hurt":
		animated_sprite.play("hurt")

func _process_dead_state(_delta):
	velocity = Vector2.ZERO
	if not animated_sprite.animation == "death":
		animated_sprite.play("death")
	# 确保死亡状态下不会被重新激活
	set_physics_process(false)
	set_process(false)

func _update_patrol_location():
	var random_offset = Vector2(
		randf_range(-patrol_range, patrol_range),
		0  # 只在水平方向巡逻
	)
	patrol_location = initial_position + random_offset

func _perform_attack1():
	if not target:
		return
	
	# 确保面向玩家
	facing_right = target.global_position.x > global_position.x
	animated_sprite.flip_h = !facing_right
	
	# 启动伤害检测计时器
	attack1_damage_check_timer = attack_damage_check_frames
	attack1_damage_dealt = false
	
	# 立即检测一次伤害
	_check_attack1_damage()
	
	can_attack = false
	$AttackTimer.start(attack_cooldown)

func _check_attack1_damage():
	if not target or attack1_damage_dealt:
		return
		
	# 直接检测玩家是否在攻击范围内
	var direction_to_target = (target.global_position - global_position).normalized()
	var facing_direction = Vector2.RIGHT if facing_right else Vector2.LEFT
	var dot_product = direction_to_target.dot(facing_direction)
	
	# 检查目标是否在攻击范围内
	var distance = global_position.distance_to(target.global_position)
	
	# 近距离攻击时，忽略角度限制，只要在范围内就造成伤害
	if distance < melee_attack_range * 0.7:  # 放宽近距离判定
		if target.has_method("take_damage"):
			target.take_damage(attack1_damage, global_position)
			print("琥珀哥布林近距离普通攻击命中，造成伤害：", attack1_damage)
			attack1_damage_dealt = true  # 标记已造成伤害
			return
	
	# 正常距离攻击，放宽条件：扩大角度范围和距离范围
	if dot_product > -0.5 and distance < melee_attack_range * 1.8:  # 扩大到150度扇形和1.8倍距离
		if target.has_method("take_damage"):
			target.take_damage(attack1_damage, global_position)
			print("琥珀哥布林普通攻击命中，造成伤害：", attack1_damage)
			attack1_damage_dealt = true  # 标记已造成伤害
	else:
		print("琥珀哥布林普通攻击未命中，dot_product:", dot_product, "，距离:", distance)

func _perform_attack2():
	if not target:
		return
	
	# 确保面向玩家
	facing_right = target.global_position.x > global_position.x
	animated_sprite.flip_h = !facing_right
	
	# 获取攻击方向并设置速度 - 始终朝向玩家
	var attack_direction = (target.global_position - global_position).normalized()
	velocity.x = attack_direction.x * move_speed * 2  # 翻滚速度是正常移动速度的2倍
	
	# 强制应用速度，确保立即移动
	move_and_slide()
	
	# 启动伤害检测计时器
	attack2_damage_check_timer = attack_damage_check_frames + 10  # 给攻击2额外的检测时间
	attack2_damage_dealt = false
	
	# 立即检测一次伤害
	_check_attack2_damage()
	
	can_attack = false
	$AttackTimer.start(attack_cooldown)

func _check_attack2_damage():
	if not target or attack2_damage_dealt:
		return
		
	# 直接检测玩家是否在攻击范围内
	var distance = global_position.distance_to(target.global_position)
	
	# 近距离攻击时，无条件造成伤害
	if distance < melee_attack_range * 1.5:  # 进一步扩大基础攻击范围
		if target.has_method("take_damage"):
			target.take_damage(attack2_damage, global_position)
			print("琥珀哥布林近距离翻滚攻击命中，造成伤害：", attack2_damage)
			attack2_damage_dealt = true  # 标记已造成伤害
			_play_hit_effect(target.global_position)  # 播放命中效果
			return
	
	# 正常距离攻击，大幅扩大检测范围
	if distance < melee_attack_range * 3.5:  # 进一步增加翻滚攻击范围
		# 检查是否在攻击方向上 - 简化检测逻辑
		var attack_dir = Vector2(velocity.x, 0).normalized()
		var valid_hit = false
		
		# 如果速度很大，说明正在翻滚中，几乎任何角度都可命中
		if abs(velocity.x) > move_speed:
			valid_hit = true
		else:
			# 如果速度较小，检查是否在攻击方向上
			var dir_to_target = (target.global_position - global_position).normalized()
			var dot = dir_to_target.dot(attack_dir)
			valid_hit = (dot > -0.8)  # 更宽松的角度判定
		
		if valid_hit:
			if target.has_method("take_damage"):
				target.take_damage(attack2_damage, global_position)
				print("琥珀哥布林翻滚攻击命中，造成伤害：", attack2_damage)
				attack2_damage_dealt = true  # 标记已造成伤害
				_play_hit_effect(target.global_position)  # 播放命中效果
	else:
		print("琥珀哥布林翻滚攻击未命中，距离:", distance)

# 添加命中效果函数
func _play_hit_effect(hit_position):
	# 这里可以添加命中的粒子效果或动画
	# 例如：
	# var hit_effect = hit_effect_scene.instantiate()
	# hit_effect.global_position = hit_position
	# get_tree().current_scene.add_child(hit_effect)
	pass

func _perform_attack3():
	can_attack = false
	# 不在这里实例化炸弹，而是在动画结束时
	$AttackTimer.start(attack_cooldown * 1.5)  # 扔炸弹冷却时间更长

func take_damage(amount: float, attacker_position: Vector2 = Vector2.ZERO, is_quick_slash: bool = false) -> void:
	if current_state == State.DEAD or is_invincible:
		return
	
	current_health -= amount
	is_invincible = true
	invincible_timer = INVINCIBLE_TIME
	modulate.a = 0.5
	
	# 更新血条
	if health_bar:
		health_bar.set_health(current_health, max_health)
	
	# 计算击退方向和力度
	if attacker_position != Vector2.ZERO:
		var knockback_direction = (global_position - attacker_position).normalized()
		
		if is_quick_slash:
			# 疾风斩带来更强的击退效果
			knockback_force = knockback_direction * 400  # 更强的水平击退
			knockback_force.y = -250  # 更高的向上力度
		else:
			knockback_force = knockback_direction * 300
			knockback_force.y = -200  # 添加向上的力
	
	if current_health <= 0:
		current_state = State.DEAD
		animated_sprite.play("death")
		# 禁用碰撞
		$CollisionShape2D.set_deferred("disabled", true)
		if attack_hitbox:
			attack_hitbox.set_deferred("monitoring", false)
			attack_hitbox.set_deferred("monitorable", false)
		if hurt_box:
			hurt_box.set_deferred("monitoring", false)
			hurt_box.set_deferred("monitorable", false)
		# 确保在动画结束后销毁
		if not animated_sprite.animation_finished.is_connected(_on_death_animation_finished):
			animated_sprite.animation_finished.connect(_on_death_animation_finished)
	else:
		# 对于疾风斩，跳过hurt状态，只应用视觉效果
		if is_quick_slash:
			# 闪光效果
			var tween = create_tween()
			tween.tween_property(self, "modulate", Color(2.0, 1.5, 1.5, 0.7), 0.1)
			tween.tween_property(self, "modulate", Color(1, 1, 1, 0.5), 0.1)
		else:
			current_state = State.HURT
			animated_sprite.play("hurt")
			$StateTimer.start(0.3)

func _on_death_animation_finished():
	if animated_sprite.animation == "death":
		queue_free()

func _on_detection_area_body_entered(body):
	if body.is_in_group("player"):
		target = body
		last_target_position = body.global_position
		chase_memory_timer = chase_memory_time
		if current_state != State.ATTACK1 and current_state != State.ATTACK2 and current_state != State.ATTACK3:
			current_state = State.CHASE

func _on_detection_area_body_exited(body):
	if body == target:
		target = null
		animated_sprite.play("idle")

func _on_attack_timer_timeout():
	can_attack = true
	if target:
		var distance = global_position.distance_to(target.global_position)
		if distance < melee_attack_range:  # 使用更大的近战范围
			velocity.x = 0  # 确保停止移动
			
			# 攻击前确保面向玩家
			facing_right = target.global_position.x > global_position.x
			animated_sprite.flip_h = !facing_right
			
			attack_type = randi_range(1, 2)
			current_state = State.ATTACK1 if attack_type == 1 else State.ATTACK2
			animated_sprite.play("attack" + str(attack_type))
		elif distance < bomb_attack_range:
			velocity.x = 0  # 确保停止移动
			
			# 攻击前确保面向玩家
			facing_right = target.global_position.x > global_position.x
			animated_sprite.flip_h = !facing_right
			
			attack_type = 3
			current_state = State.ATTACK3
			animated_sprite.play("attack3")
		else:
			# 如果超出攻击范围，进入追击状态
			current_state = State.CHASE
			
			# 但不立即冲向玩家，保持一定距离
			if distance > bomb_attack_range * 1.2:
				# 如果远超攻击范围，正常追击
				velocity.x = (target.global_position - global_position).normalized().x * move_speed
				animated_sprite.play("run")
				# 不强制面向玩家，让_physics_process根据移动方向决定朝向
			else:
				# 在攻击范围附近，保持静止
				velocity.x = 0
				animated_sprite.play("idle")
				# 静止时可以面向玩家
				facing_right = target.global_position.x > global_position.x
				animated_sprite.flip_h = !facing_right

func _on_state_timer_timeout():
	if current_state == State.HURT:
		current_state = State.CHASE if target else State.IDLE
		animated_sprite.play("run" if target else "idle")

func _on_animated_sprite_animation_finished():
	match animated_sprite.animation:
		"attack1":
			attack_ready = false  # 重置攻击状态
			current_state = State.CHASE
			animated_sprite.play("run")
			
			# 攻击后根据与目标的距离决定行为
			if target:
				var distance = global_position.distance_to(target.global_position)
				# 根据距离决定行为
				if distance < preferred_attack_distance * 0.8:
					# 如果太近，后退
					velocity.x = -(target.global_position - global_position).normalized().x * move_speed * 0.5
				elif distance > preferred_attack_distance * 1.2 and distance < bomb_attack_range:
					# 如果在攻击范围内但距离不理想，缓慢接近
					velocity.x = (target.global_position - global_position).normalized().x * move_speed * 0.3
				else:
					# 在理想距离或超出攻击范围
					velocity.x = 0
			else:
				velocity.x = 0
				
		"attack2":
			attack_ready = false  # 重置攻击状态
			current_state = State.CHASE
			animated_sprite.play("run")
			
			# 翻滚攻击后强制停下来
			velocity.x = 0
			
			# 如果还有目标，面向目标
			if target:
				facing_right = target.global_position.x > global_position.x
				animated_sprite.flip_h = !facing_right
		"attack3":
			# 在扔炸弹动画结束时实例化炸弹
			if bomb_scene:  # 只要有bomb_scene就尝试实例化炸弹
				var bomb = bomb_scene.instantiate()
				
				# 设置炸弹位置 - 从哥布林手中扔出
				var spawn_offset = Vector2(20, -10)  # 调整这个值以匹配动画
				if !facing_right:
					spawn_offset.x = -spawn_offset.x
				bomb.global_position = global_position + spawn_offset
				
				# 计算目标位置 - 使用最后记录的目标位置
				var target_pos = target.global_position if target else last_target_position
				
				# 计算距离
				var distance_to_target = global_position.distance_to(target_pos)
				
				# 设置炸弹目标位置 - 不再额外添加距离
				bomb.target = target_pos
				
				# 设置炸弹参数 - 根据距离调整投掷行为
				bomb.damage = bomb_damage
				bomb.throw_force = min(400.0, 200.0 + distance_to_target * 0.5)  # 距离越远，力度越大，但有上限
				
				# 近距离时使用更高的抛物线
				if distance_to_target < 120:
					bomb.throw_arc_height = 250.0
					bomb.throw_height_factor = 1.5
					bomb.close_range = true  # 添加一个标记，表明这是近距离投掷
				else:
					bomb.throw_arc_height = 200.0
					bomb.throw_height_factor = 1.0
					bomb.close_range = false
				
				# 设置炸弹动画
				if bomb.animated_sprite:
					bomb.animated_sprite.play("bomb1")  # 使用 amber 哥布林的炸弹动画
					bomb.is_ashcurse = false  # 明确设置为非诅咒炸弹
					
				# 添加到场景中
				get_parent().add_child(bomb)
				print("琥珀哥布林扔出炸弹，目标位置: ", target_pos, "，距离: ", distance_to_target)
			
			attack_ready = false  # 重置攻击状态
			current_state = State.CHASE  # 总是回到追击状态
			animated_sprite.play("run")
		"death":
			queue_free()  # 确保在死亡动画结束后销毁实例

func _on_hurt_box_area_entered(area: Area2D):
	if area.is_in_group("player_attack"):
		var damage = area.get_parent().get("attack_damage") if area.get_parent().has_method("get") else 10.0
		
		# 检查是否是疾风斩攻击
		var is_quick_slash = false
		if area.get_parent() and area.get_parent().has_method("is_player") and area.get_parent().get("is_quick_slashing") != null:
			is_quick_slash = area.get_parent().is_quick_slashing
		
		take_damage(damage, area.global_position, is_quick_slash)
